//
//  WmcNotificationManager.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/20.
//

import Foundation

enum WmcNotificationType: String {
    case createTeamSucceed
    case joinTeamSucceed
    case addTeamMemberSucceed
    case deleteTeamMemberSucceed
    case editTeamNameSucceed
    // 编辑用户名字
    case editUserNameSucceed
    case editTeamRemarkSucceed
    case editUserAvatarSucceed
    /// 点击 去拍照试一试
    case clickedGotoShoot
    ///  成员 退出团队
    case memberQuitTeam = "deleteTeamMember"
    /// 删除照片成功
    case deletePhotoSucceed
    /// 同步开关
    case synSettingChanged
}

class WmcNotificationManager: NSObject {
    static let shared = WmcNotificationManager()

    /// 注册通知 -- 界面通知。vc dealloc的时候remove obs
    /// - Parameters:
    ///   - type: YPNotificationType.enum
    ///   - owner: 何处通知拥有
    ///   - receiveHandler: 接收userinfo的回调
    func addObserver(type: WmcNotificationType,
                     owner: UIViewController,
                     receiveHandler: ((Notification) -> Void)?)
    {
        let name = type.rawValue
        let notificationName = Notification.Name(rawValue: name)
        _ = NotificationCenter.default.rx.notification(notificationName).takeUntil(owner.rx.deallocated).subscribe(onNext: { [weak self] noti in
            // 处理通知
            self?.handleInfo(info: noti, receiveHandler: receiveHandler)
        })
    }

    /// 注册通知 -- 临时通知。注册一次，接受一次
    /// - Parameters:
    ///   - type: YPNotificationType.enum
    ///   - receiveHandler: 接收userinfo的回调
    func addObserver(type: WmcNotificationType,
                     receiveHandler: ((Notification) -> Void)?)
    {
        let name = type.rawValue
        let notificationName = Notification.Name(rawValue: name)
        _ = NotificationCenter.default.rx.notification(notificationName).subscribe(onNext: { [weak self] noti in
            guard let self = self else { return }
            // 处理通知
            self.handleInfo(info: noti, receiveHandler: receiveHandler)
            // 移除通知
            NotificationCenter.default.removeObserver(self, name: notificationName, object: nil)
        })
    }

    /// 注册通知 -- 不自动remove
    /// - Parameters:
    ///   - type: type
    ///   - receiveHandler: 接收userinfo的回调
    func addObserverWithoutRemove(type: WmcNotificationType,
                                  receiveHandler: ((Notification) -> Void)?)
    {
        let name = type.rawValue
        let notificationName = Notification.Name(rawValue: name)
        _ = NotificationCenter.default.rx.notification(notificationName).subscribe(onNext: { [weak self] noti in
            guard let self = self else { return }
            // 处理通知
            self.handleInfo(info: noti, receiveHandler: receiveHandler)
        })
    }

    func removeObs(type: WmcNotificationType) {
        let name = type.rawValue
        let notificationName = Notification.Name(rawValue: name)
        // 移除通知
        NotificationCenter.default.removeObserver(self, name: notificationName, object: nil)
    }

    /// 处理通知
    /// - Parameters:
    ///   - info: info
    ///   - receiveHandler: 回调
    func handleInfo(info: Notification,
                    receiveHandler: ((Notification) -> Void)?)
    {
        receiveHandler?(info)
//        let name = info.name
    }

    /// 发送通知
    /// - Parameters:
    ///   - type: info
    ///   - object: object
    func post(type: WmcNotificationType,
              object: Any?)
    {
        let name = type.rawValue
        let notificationName = Notification.Name(rawValue: name)
        NotificationCenter.default.post(name: notificationName, object: object)
    }

    /// 发送通知
    /// - Parameters:
    ///   - type: info
    ///   - object: object
    static func post(type: WmcNotificationType, object: Any?) {
        let name = type.rawValue
        let notificationName = Notification.Name(rawValue: name)
        NotificationCenter.default.post(name: notificationName, object: object)
    }
}
